package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.Condition;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.SmsBannerBook;
import com.juneng.mapper.SmsBannerBookMapper;
import com.juneng.service.ISmsBannerBookService;
import com.juneng.dto.SmsBannerBookDto;
import com.juneng.dto.SmsBannerBookQueryDto;
import com.juneng.vo.SmsBannerBookVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import java.util.List;
/** share
 * 书籍轮播图表 服务实现类
 * attention  fans、 attention dynamic
 * @author zds
 * @since 2021-07-15
 */
@Service
@AllArgsConstructor
public class SmsBannerBookServiceImpl implements ISmsBannerBookService {

    protected SmsBannerBookMapper smsBannerBookMapper;
    @Override
    public IPage<SmsBannerBookVo> page(SmsBannerBookQueryDto dto) {
        IPage<SmsBannerBookVo> page = Condition.getPage(dto);
        return smsBannerBookMapper.page(page,dto);
    }

    @Override
    public List<SmsBannerBook> list(SmsBannerBookQueryDto dto) {
        QueryWrapper<SmsBannerBook> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,SmsBannerBook.class));
        return smsBannerBookMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(SmsBannerBookDto dto) {
        List<SmsBannerBook> smsBannerBooks = smsBannerBookMapper.selectList(new LambdaQueryWrapper<SmsBannerBook>().eq(SmsBannerBook::getBookId, dto.getBookId()));
        for (SmsBannerBook smsBannerBook : smsBannerBooks) {
            smsBannerBookMapper.deleteById(smsBannerBook.getId());
        }
        for (String s : dto.getBannerUrl()) {
            dto.setBannerPicture(s);
            smsBannerBookMapper.insert(BeanCopyUtils.copy(dto,SmsBannerBook.class));
        }
        return 1;
    }

    @Override
    public Integer updateById(SmsBannerBookDto dto) {
        return smsBannerBookMapper.updateById(BeanCopyUtils.copy(dto,SmsBannerBook.class));
    }

    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return smsBannerBookMapper.deleteBatchIds(toIntList);
    }

    @Override
    public SmsBannerBook getById(Integer id) {
        return smsBannerBookMapper.selectById(id);
    }
}
